<template>
  <a-form :style="{width:'50%',margin:'auto',padding:'50px 0'}"
          id="components-form-demo-normal-login"
          :form="form"
          class="login-form"
          @submit="handleSubmit">
    <a-form-item>
      <a-input v-decorator="[
          'userName',
          { rules: [{ required: true, message: 'Please input your username!' }] },
        ]"
               placeholder="Username">
        <a-icon slot="prefix"
                type="user"
                style="color: rgba(0,0,0,.25)" />
      </a-input>
    </a-form-item>
    <a-form-item>
      <a-input v-decorator="[
          'pwd',
          { rules: [{ required: true, message: 'Please input your Password!' }] },
        ]"
               type="password"
               placeholder="Password">
        <a-icon slot="prefix"
                type="lock"
                style="color: rgba(0,0,0,.25)" />
      </a-input>
    </a-form-item>
    <a-form-item>
      <a-button type="primary"
                html-type="submit"
                class="login-form-button">
        登录
      </a-button>
    </a-form-item>
  </a-form>
</template>

<script>
import { isLogin } from '@api'
export default {
  beforeCreate () {
    this.form = this.$form.createForm(this, { name: 'normal_login' })
  },
  methods: {
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          isLogin(values).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg, 1, () => {
                localStorage.setItem('token', res.data.data.token)
                localStorage.setItem('userInfo', JSON.stringify(res.data.data.userInfo))
                this.$router.replace('/')
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style>
#components-form-demo-normal-login .login-form {
  max-width: 300px;
}
#components-form-demo-normal-login .login-form-forgot {
  float: right;
}
#components-form-demo-normal-login .login-form-button {
  width: 100%;
}
</style>
